package com.example.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.example.commonutils.R;
import com.example.servicebase.exceptionhandler.ZDYException;
import com.example.vod.service.VodService;
import com.example.vod.utils.ConstantVodUtils;
import com.example.vod.utils.InitVideoClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Iterator;
import java.util.List;

/**
 * @author LUOXIN
 * @create 2021-09-12 21:51
 */


@RestController
@RequestMapping("/edu_vod/edu_video")
public class VodController {
    @Autowired
    private VodService vodService;


    @PostMapping("uploadVideo")
    public R uploadVideo(MultipartFile file){
        System.out.println(file.getName());
        String videoId=vodService.uploadVideo(file);

        return R.ok().data("videoId",videoId);

    }

    @DeleteMapping("deleteVideo/{id}")
    public R deleteVideo(@PathVariable String id) {
        try {
            DefaultAcsClient client = InitVideoClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);

            DeleteVideoRequest request = new DeleteVideoRequest();
//2d042964cf1d4e0eb00948b365e97fe6
            request.setVideoIds(id);
            client.getAcsResponse(request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ZDYException(20001, "删除视频失败");
        }
    }

    @DeleteMapping("deleteVideoList")
    public R deleteVideoList(@PathVariable("videoList") List<String> videoList){

        Iterator iterator = videoList.iterator();
        while (iterator.hasNext()){
            String videoId= (String) iterator.next();
            deleteVideo(videoId);
        }
        return R.ok();
    }

    @DeleteMapping("delete-batch")
    public R deleteVideoes(@PathVariable("videoList") List<String> videoList){
        vodService.deleteVideos(videoList);

        return R.ok();
    }

    @GetMapping("getPlayAuth/{id}")
    public R getPlayAuth(@PathVariable String id) {
        try {
            //创建初始化对象
            DefaultAcsClient client =
                    InitVideoClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建获取凭证request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request设置视频id
            request.setVideoId(id);
            //调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            return R.ok().data("playAuth",playAuth);
        }catch(Exception e) {
            throw new ZDYException(20001,"获取凭证失败");
        }
    }
}
